package com.suarte.core;

import org.appfuse.model.BaseObject;

import javax.persistence.*;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import org.appfuse.model.User;

/**
 * Created by IntelliJ IDEA.
 * User: Gcastillo
 * Date: Dec 30, 2010
 * Time: 4:22:01 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "sa_invoices")
public class Invoice extends BaseObject {

    private Long id;
    private Company company;
    private Contact contact;
    private Quotation quotation;
    private Date date;
    private String number;
    private String symbol;
    private Float total;
    private Float subTotal;
    private Float tax;
    private String notes;
    private Employee employee;
    private String description;
    private DocumentStatus status = DocumentStatus.ACTIVA;
    private List<InvoiceDetail> details = new ArrayList();
    private Date expirationDate;
    private Currency currency;
    private Currency exchangeCurrency;
    private Float exchangeRate;
    private Float equivalentAmount;
    private Float localAmount;
    private Float foreignAmount;
    private Float amountPaid;
    private Float localAmountPaid;
    private Float foreignAmountPaid;
    private Float currentPayment;
    private User createdBy;
    private Date createdOn = new Date();
    private User modifiedBy;
    private Date modifiedOn = new Date();
    private Long oldId;

    public Invoice() {
        equivalentAmount = 0f;
        amountPaid = 0f;
        localAmountPaid = 0f;
        foreignAmountPaid = 0f;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "INVOICE_SEQ")
    @SequenceGenerator(name = "INVOICE_SEQ", sequenceName = "INVOICE_SEQ", allocationSize = 1)
    public Long getId() {
        return id;
    }

    @ManyToOne
    @JoinColumn(name = "company_id")
    public Company getCompany() {
        return company;
    }

    @ManyToOne
    @JoinColumn(name = "contact_id")
    public Contact getContact() {
        return contact;
    }

    @ManyToOne
    @JoinColumn(name = "payment_method_id")
    public Quotation getQuotation() {
        return quotation;
    }

    @Column(name = "invoice_date")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getDate() {
        return date;
    }

    @Column(name = "invoice_number")
    public String getNumber() {
        return number;
    }

    @Column(name = "symbol")
    public String getSymbol() {
        return symbol;
    }

    @Column(name = "total")
    public Float getTotal() {
        return total;
    }

    @Column(name = "tax")
    public Float getTax() {
        return tax;
    }

    @Column(name = "subtotal")
    public Float getSubTotal() {
        return subTotal;
    }

    @Column(name = "notes")
    public String getNotes() {
        return notes;
    }

    @ManyToOne
    @JoinColumn(name = "employee_id")
    public Employee getEmployee() {
        return employee;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "status")
    public DocumentStatus getStatus() {
        return status;
    }

    @Column(name = "expiration_date")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getExpirationDate() {
        return expirationDate;
    }

    @ManyToOne
    @JoinColumn(name = "currency_id")
    public Currency getCurrency() {
        return currency;
    }

    @ManyToOne
    @JoinColumn(name = "exchange_currency_id")
    public Currency getExchangeCurrency() {
        return exchangeCurrency;
    }

    @Column(name = "exchange_rate")
    public Float getExchangeRate() {
        return exchangeRate;
    }

    @Column(name = "equivalent_amount")
    public Float getEquivalentAmount() {
        return equivalentAmount;
    }

    @OneToMany(mappedBy = "invoice", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public List<InvoiceDetail> getDetails() {
        return details;
    }

    @Column(name = "local_amount")
    public Float getLocalAmount() {
        return localAmount;
    }

    @Column(name = "foreign_amount")
    public Float getForeignAmount() {
        return foreignAmount;
    }

    @Column(name = "amount_paid")
    public Float getAmountPaid() {
        return amountPaid;
    }

    @Column(name = "local_amount_paid")
    public Float getLocalAmountPaid() {
        return localAmountPaid;
    }

    @Column(name = "foreign_amount_paid")
    public Float getForeignAmountPaid() {
        return foreignAmountPaid;
    }

    @ManyToOne
    @JoinColumn(name = "created_by")
    public User getCreatedBy() {
        return createdBy;
    }

    @ManyToOne
    @JoinColumn(name = "modified_by")
    public User getModifiedBy() {
        return modifiedBy;
    }

    @Column(name = "created_on")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getCreatedOn() {
        return createdOn;
    }

    @Column(name = "modified_on")
    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getModifiedOn() {
        return modifiedOn;
    }

    @Column(name = "old_id")
    public Long getOldId() {
        return oldId;
    }

    @Column(name = "current_payment")
    public Float getCurrentPayment() {
        return currentPayment;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public void setQuotation(Quotation quotation) {
        this.quotation = quotation;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public void setTotal(Float total) {
        this.total = total;
    }

    public void setTax(Float tax) {
        this.tax = tax;
    }

    public void setSubTotal(Float subTotal) {
        this.subTotal = subTotal;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setStatus(DocumentStatus status) {
        this.status = status;
    }

    public void setDetails(List<InvoiceDetail> details) {
        this.details = details;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }

    public void setCurrency(Currency currency) {
        this.currency = currency;
    }

    public void setExchangeCurrency(Currency exchangeCurrency) {
        this.exchangeCurrency = exchangeCurrency;
    }

    public void setExchangeRate(Float exchangeRate) {
        this.exchangeRate = exchangeRate;
    }

    public void setEquivalentAmount(Float equivalentAmount) {
        this.equivalentAmount = equivalentAmount;
    }

    public void setLocalAmount(Float localAmount) {
        this.localAmount = localAmount;
    }

    public void setForeignAmount(Float foreignAmount) {
        this.foreignAmount = foreignAmount;
    }

    public void setAmountPaid(Float amountPaid) {
        this.amountPaid = amountPaid;
    }

    public void setLocalAmountPaid(Float localAmountPaid) {
        this.localAmountPaid = localAmountPaid;
    }

    public void setForeignAmountPaid(Float foreignAmountPaid) {
        this.foreignAmountPaid = foreignAmountPaid;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public void setModifiedBy(User modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    public void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public void setModifiedOn(Date modifiedOn) {
        this.modifiedOn = modifiedOn;
    }

    public void setOldId(Long oldId) {
        this.oldId = oldId;
    }

    public void setCurrentPayment(Float currentPayment) {
        this.currentPayment = currentPayment;
    }
    
    @Override
    public String toString() {
        return description;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if (!(other instanceof Invoice)) {
            return false;
        }
        Invoice _i = (Invoice) other;
        return id.equals(_i.id);
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    public void addDetail(InvoiceDetail detail) {
        detail.setInvoice(this);
        details.add(detail);
    }
}
